document.addEventListener('DOMContentLoaded', function() {
  const screenshotBtn = document.getElementById('screenshotBtn');
  const statusDiv = document.getElementById('status');
  
  screenshotBtn.addEventListener('click', async function() {
    try {
      // 更新状态
      statusDiv.textContent = '正在截屏...';
      screenshotBtn.disabled = true;
      screenshotBtn.textContent = '截屏中...';
      
      // 获取当前活动标签页
      const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
      
      // 发送消息给background script来执行截屏
      chrome.runtime.sendMessage({
        action: 'takeScreenshot',
        tabId: tab.id
      }, function(response) {
        if (response && response.success) {
          statusDiv.textContent = '截屏成功！文件已保存到下载文件夹';
          statusDiv.style.color = '#4CAF50';
        } else {
          statusDiv.textContent = '截屏失败，请重试';
          statusDiv.style.color = '#f44336';
        }
        
        // 恢复按钮状态
        setTimeout(() => {
          screenshotBtn.disabled = false;
          screenshotBtn.textContent = '截取当前页面';
          statusDiv.textContent = '点击按钮开始截屏';
          statusDiv.style.color = '';
        }, 2000);
      });
      
    } catch (error) {
      console.error('截屏错误:', error);
      statusDiv.textContent = '截屏失败: ' + error.message;
      statusDiv.style.color = '#f44336';
      
      // 恢复按钮状态
      setTimeout(() => {
        screenshotBtn.disabled = false;
        screenshotBtn.textContent = '截取当前页面';
        statusDiv.textContent = '点击按钮开始截屏';
        statusDiv.style.color = '';
      }, 2000);
    }
  });
});